<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>Card name</span>
      </div>
    </template>
    <!--    角色列表  -->
    <el-table :data="roles" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="#" width="180" />
      <el-table-column prop="rname" label="角色名称" width="180" />
      <el-table-column prop="nbl" label="角色状态" width="180" />
      <el-table-column label="角色状态" width="180">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>name: {{ scope.row.rname }}</div>
              <div>address: {{ scope.row.nbl }}</div>
            </template>
            <template #reference>
              <el-tag>
                <el-switch
                  v-model="scope.row.nbl"
                  style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                  @click="hadleNblSwitch(scope.$index, scope.row)"
                  :active-value="1"
                  :inactive-value="0"
                />
              </el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
    </el-table>

    <template #footer>Footer content</template>
  </el-card>
</template>

<script setup>
import { useRoles } from '../../stores/role.js'
import { storeToRefs } from 'pinia'
import { onMounted } from 'vue'

const roleStore = useRoles()

const hadleNblSwitch = (index, row) => {
  roleStore.hadleNblSwitch(index, row)
}

const { roles } = storeToRefs(roleStore)
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 3 === 1) {
    return 'warning-row'
  } else if (rowIndex % 3 === 2) {
    return 'success-row'
  }
  return ''
}

onMounted(() => {
  roleStore.loaddata()
})
</script>

<style>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
</style>
